//
//  CustomGuiGeView.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/2.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "CustomGuiGeView.h"

#import "FenLeiSelectListModle.h"

@implementation CustomGuiGeView
{
    int numPay;//购买的数量
    NSString *btnTitle;//选择的规格
    
    NSArray *tempArray;
}
-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
      self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
        numPay = 1;
        _priceOne = @"0.0";
     
        
        //弹起：
        [[NSNotificationCenter defaultCenter] addObserver:self
            
                                                    selector:@selector(keyboardWillShow:)
                                                        name:UIKeyboardWillShowNotification
                                                      object:nil];
        //回收：
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                    selector:@selector(keyboardWillHide:)
                                                        name:UIKeyboardWillHideNotification
                                                      object:nil];
    }
    return self;
}
- (void)setKindsDataArray:(NSArray *)kindsDataArray {
    _kindsDataArray = kindsDataArray;
}

- (void)setNumArray:(NSArray *)numArray {
    _numArray = numArray;
    
    FenLeiSelectListModle *modle = [_numArray firstObject];
    tempArray = modle.value; //[_numArray firstObject][@"value"];
    
    if (tempArray.count == 0) {
        tempArray = @[@"暂无其他规格"];
        _guigelb.text = @"请选择规格";
    }
    
    [self configureView];
    
}

//配置view
- (void)configureView {
  
    _bgV = [[UIView alloc] initWithFrame:CGRectMake(0, kView_Height-350, kView_Width, 350)];
    _bgV.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgV];
    
    _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(15, kView_Height - 380, 90, 90)];
    _imageV.image = [UIImage imageNamed:@"满20减3"];
    [self addSubview:_imageV];
    
    _priceLB =[[UILabel alloc] initWithFrame:CGRectMake( CGRectGetMaxX(_imageV.frame)+10, 10,100, 25)];
    _priceLB.textColor = [UIColor redColor];
    _priceLB.text = [NSString stringWithFormat:@"价格¥%@",_priceOne];
    [_bgV addSubview:_priceLB];
    
    _guigelb = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(_priceLB.frame), CGRectGetMaxY(_priceLB.frame), 100, 25)];
    _guigelb.textColor = [UIColor blackColor];
    _guigelb.text = @"请选择规格";
    _guigelb.font = kFont15;
    [_bgV addSubview:_guigelb];
    
    
    
    
    
    UIButton *clooseBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    clooseBtn.frame = CGRectMake(kView_Width - 40, 10, 30, 30);
    [clooseBtn setImage:[UIImage imageNamed:@"sousuo_cha"] forState:(UIControlStateNormal)];
    [_bgV addSubview:clooseBtn];
    [clooseBtn addTarget:self action:@selector(handleClooseBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    
    UIView *oneV = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_guigelb.frame)+15, kView_Width - 20, 1)];
    oneV.backgroundColor = KShouYeBg;
    [_bgV addSubview:oneV];
    
    
    
    
    //购买重量View
    UILabel *labTw = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(oneV.frame)+20, 100, 25)];
    labTw.textColor = [UIColor blackColor];
    FenLeiSelectListModle *modle = [_numArray firstObject];
    labTw.text = modle.name;
    [_bgV addSubview:labTw];
    
    
    
    
    CGFloat y = CGRectGetMaxY(labTw.frame)+15;
   CGFloat x = 10;
    for (int i = 0 ; i < tempArray.count; i++) {
        UIButton *btn2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [btn2 setTitle:tempArray[i] forState:(UIControlStateNormal)];
        btn2.titleLabel.font = kFont14;
        btn2.tag = 101 + i;
      
        btn2.frame = CGRectMake(x, y, 100, 30);
        btn2.layer.masksToBounds = YES;
        btn2.layer.cornerRadius = 3;
        [btn2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [btn2 setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        [btn2 setBackgroundImage:[UIImage imageNamed:@"anniu"] forState:(UIControlStateNormal)];
         [btn2 setBackgroundImage:[UIImage imageNamed:@"bg1"] forState:(UIControlStateSelected)];
        
       
        [btn2 addTarget:self action:@selector(handleChooseBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_bgV addSubview:btn2];
        
        x = x+15+100;
    }
    
    
    UIView *twoV = [[UIView alloc] initWithFrame:CGRectMake(10, y+30+15, kView_Width - 20, 1)];
    twoV.backgroundColor = KShouYeBg;
    [_bgV addSubview:twoV];
    
    
    UILabel *payNumL = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(twoV.frame)+15, 100, 30)];
    payNumL.text = @"购买数量";
    payNumL.textColor = [UIColor blackColor];
    [_bgV addSubview:payNumL];
    
    
    
    
    //购买数量View
    UIView *countView = [[UIView alloc] initWithFrame:CGRectMake(kView_Width - 130, CGRectGetMinY(payNumL.frame), 120, 30)];
    countView.layer.masksToBounds = YES;
    countView.layer.cornerRadius =3;
    countView.layer.borderColor = KShouYeBg.CGColor;
    countView.layer.borderWidth = 1.0f;
    [_bgV addSubview:countView];
    
    
    UIButton *jianBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    jianBtn.frame = CGRectMake(0, 0, 30, 30);
    [jianBtn setImage:[UIImage imageNamed:@"gouwuche_jian2"] forState:(UIControlStateNormal)];
    jianBtn.tag = 201;
    [jianBtn addTarget:self action:@selector(handlePayNumAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [countView addSubview:jianBtn];
    
    _numTF = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(jianBtn.frame)+10, 0, CGRectGetWidth(countView.frame)- 30-30-10-10, 30)];
    _numTF.textColor = [UIColor blackColor];
    _numTF.text = [NSString stringWithFormat:@"%d",numPay];
    _numTF.textAlignment = NSTextAlignmentCenter;
    _numTF.font = kFont14;
    _numTF.delegate = self;
    _numTF.keyboardType =UIKeyboardTypeNumberPad;
     [_numTF addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    [countView addSubview:_numTF];
    
    
    UIButton *addBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    addBtn.frame = CGRectMake(CGRectGetWidth(countView.frame) - 30, 0, 30, 30);
    [addBtn setImage:[UIImage imageNamed:@"gouwuche_jia"] forState:(UIControlStateNormal)];
    addBtn.tag = 202;
    [addBtn addTarget:self action:@selector(handlePayNumAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [countView addSubview:addBtn];
    
    
    
    UIView *thirdV = [[UIView alloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(countView.frame)+15, kView_Width - 20, 1)];
    thirdV.backgroundColor = KShouYeBg;
    [_bgV addSubview:thirdV];
    
    
    UILabel *payMoneyLB = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(thirdV.frame)+15, 100, 30)];
    payMoneyLB.text = @"总计";
    payMoneyLB.textColor = [UIColor blackColor];
    [_bgV addSubview:payMoneyLB];
    
    
    _countMoeny  = [[UILabel alloc] initWithFrame:CGRectMake(kView_Width - 200, CGRectGetMinY(payMoneyLB.frame), 190, 30)];
    _countMoeny.text = @"¥100.0";
    _countMoeny.textAlignment = NSTextAlignmentRight;
    _countMoeny.textColor = [UIColor redColor];
    [_bgV addSubview:_countMoeny];
    
    
    
    
    
    //确认购买按钮
    UIButton *buyBtn =[UIButton buttonWithType:(UIButtonTypeCustom)];
    buyBtn.frame = CGRectMake(0, CGRectGetHeight(_bgV.frame)- 50, kView_Width, 50);
    [buyBtn setTitle:@"确定" forState:(UIControlStateNormal)];
    [buyBtn setBackgroundImage:[UIImage imageNamed:@"bg1"] forState:(UIControlStateNormal)];
    [_bgV addSubview:buyBtn];
    
    [buyBtn addTarget:self action:@selector(handleBuyBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
}


//选择的数量
- (void)handlePayNumAction :(UIButton *)sender {
    numPay = [_numTF.text intValue];
    CGFloat money =[_priceOne floatValue];
    if (sender.tag == 201) {
        if (numPay< [_minNum intValue]+1) {
             [self showTextNoActivity:[NSString stringWithFormat:@"起订量 %@",_minNum] timeLength:2];
            return;
        }
        numPay--;
        _numTF.text = [NSString stringWithFormat:@"%d",numPay];
        
        _countMoeny.text = [NSString stringWithFormat:@"¥%.1f", money *numPay];
        
    }
    
    if (sender.tag == 202) {
        numPay++;
        if ( numPay >[_countNumChoose intValue]) {
             [self showTextNoActivity:[NSString stringWithFormat:@"库存仅剩 %@",_countNumChoose] timeLength:2];
            return;
        }
        _numTF.text = [NSString stringWithFormat:@"%d",numPay];
        _countMoeny.text = [NSString stringWithFormat:@"¥%.1f", money *numPay];
    }
}

//实时获取输入框的值
- (void)textFieldEditChanged:(UITextField *)text {
     numPay = [text.text intValue];
    if (numPay > [_countNumChoose intValue]) {
        [self showTextNoActivity:[NSString stringWithFormat:@"库存仅剩 %@",_countNumChoose] timeLength:2];
        numPay =[_countNumChoose intValue];
        CGFloat money =[_priceOne floatValue];
        _numTF.text = [NSString stringWithFormat:@"%d",numPay];
        _countMoeny.text = [NSString stringWithFormat:@"¥%.1f", money *numPay];
        return;
    }
    if (numPay < [_minNum intValue]) {
        [self showTextNoActivity:[NSString stringWithFormat:@"起订量 %@",_minNum] timeLength:2];
        numPay =[_minNum intValue];
        CGFloat money =[_priceOne floatValue];
        _numTF.text = [NSString stringWithFormat:@"%d",numPay];
        _countMoeny.text = [NSString stringWithFormat:@"¥%.1f", money *numPay];
        return;
    }else {
        
        CGFloat money =[_priceOne floatValue];
        _countMoeny.text = [NSString stringWithFormat:@"¥%.1f", money *numPay];
    }
}



//选择重量的按钮点击事件
- (void)handleChooseBtnAction :(UIButton *)sender {
    if (sender!= self.tempBtn) {
        self.tempBtn.selected = NO;
        sender.selected = YES;
        self.tempBtn = sender;
    }else{
        self.tempBtn.selected = !self.tempBtn.selected;
    }
    if (sender.selected == YES) {
        btnTitle = sender.titleLabel.text;
         NSLog(@"111%@",btnTitle);
        
        NSLog(@"-=-=%@",_kindsDataArray);
        for (int j  = 0; j < _kindsDataArray.count; j++) {
            
            //根据选中的条件筛选出来含有某个元素的字典,
            if ([btnTitle isEqualToString:[_kindsDataArray[j][@"sku_key"]firstObject]]) {
                _priceLB.text = [NSString stringWithFormat:@"¥%@",_kindsDataArray[j][@"sell_price"]];
                _guigelb.text = btnTitle;

                //库存
                _countNumChoose = [NSString stringWithFormat:@"%@",_kindsDataArray[j][@"store_nums"]];
                _minNum = [NSString stringWithFormat:@"%@",_kindsDataArray[j][@"minimum"]];
                
                
                //如果选择的型号的库存量不足，则显示最大的库存量并赋值给数量，重置数量的值和显示的金钱数
                if (numPay > [_countNumChoose intValue]) {
                    
                    numPay = [_countNumChoose intValue];
                    _numTF.text = [NSString stringWithFormat:@"%d",numPay];
                }
                _priceOne = [NSString stringWithFormat:@"%@",_kindsDataArray[j][@"sell_price"]];
                CGFloat money =[_priceOne floatValue];
                _countMoeny.text = [NSString stringWithFormat:@"¥%.1f", money *numPay];
                
                
            }
        }
        
    }
    //如果没有选中任何一个，则初始化所有的值
    else {
        _priceLB.text = @"¥0";
        _guigelb.text = @"请选择规格";
        btnTitle = @"请选择规格";
        _priceOne = @"0.0";
        _countNumChoose = @"10000000";
        _countMoeny.text = _priceOne;
    }
   

}




//关闭页面的按钮的点击事件
- (void)handleClooseBtn:(UIButton *)sender {
    if(self.delegate && [self.delegate respondsToSelector:@selector(clooseBtnAction:)])
    {
        [self.delegate clooseBtnAction:sender];
    }
}
//确认购买的按钮点击事件
- (void)handleBuyBtnAction :(UIButton *)sender {
    if(self.delegate && [self.delegate respondsToSelector:@selector(sureBuyBtnAction::)])
    {
        [self.delegate sureBuyBtnAction:sender :btnTitle];
    }
}



//监听键盘的弹出和回收
- (void)keyboardWillShow:(NSNotification*)notification {
        CGRect keyboardFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; //获得键盘的rect
    
    //CGRectMake(0, kView_Height-350, kView_Width, 350)
    _bgV.frame = CGRectMake(0,kView_Height-350- keyboardFrame.size.height+100, kView_Width, 350);
    _imageV.frame = CGRectMake(0,kView_Height - 380-keyboardFrame.size.height+100, 90, 90);
    
}
- (void)keyboardWillHide:(NSNotification*)notification {
    _bgV.frame = CGRectMake(0,kView_Height-350, kView_Width, 350);
    _imageV.frame = CGRectMake(0,kView_Height - 380, 90, 90);
    
}




//默认z
- (void)playValueToViews:(NSDictionary *)dic imageStr:(NSString *)imageStr{
 
    
    NSString *titleAtr =[dic[@"sku_key"] firstObject];
    self.priceLB.text =[NSString stringWithFormat:@"¥%@",dic[@"sell_price"]];
 
    if (tempArray.count == 0) {
        self.guigelb.text = @"请选择规格";
        
    }else {
       self.guigelb.text =titleAtr;
    }
    
    
   
    [self.imageV sd_setImageWithURL:[NSURL URLWithString:imageStr]];
    
    
    btnTitle = [dic[@"sku_key"] firstObject];//默认的规格名称
    
    
    //默认商品剩余的库存
    _countNumChoose =[NSString stringWithFormat:@"%@",dic[@"store_nums"]];
    
    //默认商品的出售价格
    _countMoeny.text =[NSString stringWithFormat:@"¥%@",dic[@"sell_price"]];
    
    
    _priceOne =[NSString stringWithFormat:@"%@",dic[@"sell_price"]];//默认的价格单价
    
    _minNum = [NSString stringWithFormat:@"%@",dic[@"minimum"]];//起订量
    _numTF.text = [NSString stringWithFormat:@"%@",_minNum];//起订量赋值给数量
    
    
    
    for (int i = 0 ; i< tempArray.count; i++) {
        UIButton *btn = (UIButton *)[self viewWithTag:101+i];
        if ([titleAtr isEqualToString:btn.titleLabel.text]) {
            btn.selected = YES;
            _tempBtn = btn;
        }else {
            btn.selected = NO;
        }
    }
    
}

@end
